<!--
 * @Author: your name
 * @Date: 2021-10-06 22:02:59
 * @LastEditTime: 2021-10-07 17:31:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \common-vue\src\components\mForm\MyForm.vue
-->
<template>
  <div class="MyForm-box">
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: 'MyForm',
  // 让所有子组件都能使用到顶层组件中数据
  provide() {
    return {
      form: this
    }
  },
  props: {
    // 表单数据
    model: {
      required: true,
      type: Object
    },
    // 具体校验规则
    rules: {
      type: Object
    }
  },
  methods: {
    // 校验回调
    validate(cb) {
      // 过滤,剩下myFormItem中只有prop属性,才需要校验
      const tasks = this.$children.filter(item => item.prop).map(item =>
        item.validate()
      )
      Promise.all(tasks).then(() => {
        // eslint-disable-next-line standard/no-callback-literal
        cb(true) // 所有都执行成功,该表单就通过校验
      }).catch(() => {
        console.log('false,false')
        // eslint-disable-next-line standard/no-callback-literal
        cb(false)
      })
    }
  }
}
</script>

<style scoped lang="less">

</style>
